package com.hexorld.sprite;

import java.util.ArrayList;

import android.graphics.Rect;

import com.hexorld.core.Position;
import com.hexorld.core.UpdateFlag;
import com.hexorld.layer.LayerManager;

/**
 * place sprite in right layer, manage their spawn /dead /dismiss
 * @author HJJ
 *
 */
public class SpriteManager implements ICollisionDirector {
	private LayerManager mLayerMgr = null;
	private ArrayList<HSprite> mSpriteList = new ArrayList<HSprite>();

	public SpriteManager(LayerManager lm){
		mLayerMgr = lm;
	}
	
	public void addSprite(HSprite one){
		mSpriteList.add(one);
	}
	
	public void removeSprite(HSprite one){
		if(mSpriteList.indexOf(one) != -1)
			mSpriteList.remove(one);
	}
	
	
	public void updateAll(UpdateFlag updateFlag){
		if(mSpriteList == null || mSpriteList.size() == 0)
			return;
		int count = mSpriteList.size();
		for(int i = 0; i < count; i++)
		{
			mSpriteList.get(i).update(updateFlag);
		}
	}
	
	public void updateSpriteByIndex(int index){
		if(mSpriteList != null && index < mSpriteList.size())
			mSpriteList.get(index).update(UpdateFlag.NORMAL);
	}

	@Override
	public boolean requestRect(HSprite requester, Rect rt) {
		boolean ret = true;
		for(HSprite other : mSpriteList)
		{
			if(other.collisionWithRect(rt))
			{
				boolean tmp = requester.handleCollision(other);
				ret = ret ? tmp : false;
			}
		}
		return ret;
	}
}
